/**
 * Created by Administrator on 15-7-27.
 */
var Core = require("./core.js");
var StageBox = require("./stageBox.js");
var mDrag = require("./drag.js");
console.log(mDrag)
var Drager = new mDrag({contianer:$("#seatUlWrap")});
var core = new Core();
var Stage = RichBase.extend({
	statics : {
		mouseMoveTimer : null
	},
	EVENTS : {
		"mousedown" : {
			"" : "onMouseDown"
		},
		"mouseup" : {
			"" : "onMouseUp"
		},
		"mousemove" : {
			"" : "onMouseMove"
		}
	},
	sx : 0,
	sy : 0,
	left : 0,
	top : 0,
	isDrag : false,
	init : function(opt){
		var that = this;
		this.container = opt.container;
		this.render();
		document.addEventListener("keydown",function(e){
			that.onKeyDown(that,e)
		},false)
		document.addEventListener("keyup",function(e){
			that.onKeyUp(that,e)
		},false)
	},
	getRowCount : function(){
		var val = $("#rowInp").val() || 0;
		return Number(val);
	},
	getColCount : function(){
		var val = $("#colInp").val() || 0;
		return Number(val);
	},
	getLayoutVal : function(){
		return 2;
	},
	onKeyDown : function(that,e){
		that.container.css("cursor","move");
	},
	onKeyUp : function(that,e){
		that.container.css("cursor","default");
	},
	onMouseDown : function(that,e){
		var ctrlKey = e.ctrlKey;
		if(!ctrlKey) return false;
		var container = that.container;
		that.isDrag = true;
		container.css("cursor","move");
		that.sx = e.clientX;
		that.sy = e.clientY;

	},
	onMouseUp : function(that,e){
		var ctrlKey = e.ctrlKey;
		if(!ctrlKey) return false;
		that.isDrag = false;
		var container = that.container;
		var listUl = container.children();
		var left = listUl.css("left");
		var top = listUl.css("top");
		if(left=="auto" || left=="0") left="0px";
		if(top=="auto" || top=="0") top="0px";
		left = (left.substring(0,left.length-2))*1;
		top = (top.substring(0,top.length-2))*1;
		that.left = left;
		that.top = top;
		container.css("cursor","default");
	},
	onMouseMove : function(that,e){
		var ctrlKey = e.ctrlKey;
		if(!ctrlKey || !that.isDrag) return false;
		var container = that.container;
		var listUl = container.children();
		var sx = that.sx;
		var sy = that.sy;
		var clientX = e.clientX;
		var clientY = e.clientY;
		var disX = clientX-sx;
		var disY = clientY-sy;
		var x = that.left + disX;
		var y = that.top + disY;
		if(x>0) x = 0;
		if(y>0) y = 0;
		listUl.css({
			left : x,
			top : y
		});
		container.css("cursor","move");
	},
	render : function(){
		var that = this;
		var rowCount = this.getRowCount();
		var colCount = this.getColCount();
		var layout = this.getLayoutVal();
		var colArr = core.getColArray(colCount,layout);
		var stageWidth = (StageBox.width+1) * colCount;
		var listUl = this.container.children();
		var html = "";
		for(var row=1; row<=rowCount; row++){
			for(var s in colArr){
				var col = colArr[s];
				html += new StageBox({
					col_id  : s,
					col_num : col,
					row_id  : row,
					row_num : row,
					seat    : row+"-"+col,
					seat_id : "",
					state   : "-1",
					area_id : ""
				}).render();
			}
		}
		listUl.width(stageWidth).html(html);
	}
});

module.exports = Stage;


